/* Hawk - An OSGI Based Test Framework
 *
 * Copyright (C) 2010 http://code.google.com/p/hawk-framework/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package org.hawk.gui.windows;

import java.util.ResourceBundle;

import org.hawk.gui.activator.OsgiContext;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.hawk.core.interfaces.TestEngine;

import com.vaadin.ui.Button;
import com.vaadin.ui.Window;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;

/**
 * This class creates the execute window
 * 
 * @author Rodrigo Prestes Machado
 * @version November 15, 2009
 */
@SuppressWarnings("serial")
public class ExecutionWindow extends Window {
	
	/** Store the default name of Hawk Execution Engine */
	private static final String HAWK_ENGINE_SERVICE = "org.hawk.core.engine.Engine";
	
	private ResourceBundle messages;
	
	/**
	 * Constructor 
	 * 
	 * @param ResourceBundle : The localized system messages
	 */
	public ExecutionWindow(ResourceBundle messages) {
		super(messages.getString("ExecutionWindow.title"));
		
		// Gets the reference of localized messages 
		this.messages = messages;
		
		// Configuring the execute window 
		setWidth("400");
		setHeight("250");
		center();
		setModal(true);
		
		// Adding the execution test session button
		addComponent(createExecuteButton());
	}
	
	/**
	 * This method creates the execution test session button
	 * 
	 * @return Returns the execution test session button
	 */
	private Button createExecuteButton() {
		Button executeButton = new Button(messages.getString("ExecutionWindow.buttonExecute"));
		executeButton.addListener(new ExecuteButtonListener());
		return executeButton;
	}
	
	/**
	 * Class used to handle the execute button event  
	 */
	private class ExecuteButtonListener implements ClickListener {

		/**
		 * This method is responsible to handle the execute button event. It 
		 * gets the Hawk Execution Engine and execute it 
		 * 
		 * @param The click event object 
		 */
		public void buttonClick(ClickEvent event) {
			// Gets the Hawk Engine Service
			BundleContext context = OsgiContext.getInstance().getContext();
			ServiceReference sr = context.getServiceReference(HAWK_ENGINE_SERVICE);
			
			// Executing the Hawk Engine
			TestEngine engine = (TestEngine) context.getService(sr);
			try {
				engine.run();
			} 
			catch (Exception e) {
				showNotification(messages.getString("ExecutionWindow.executionProblem"),
						Notification.TYPE_WARNING_MESSAGE);
			}
		}
	}
}